home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / cquirk.arc / CQUIRK1.DOC next >
Text File  |  1985-09-26  |  2KB  |  51 lines

  1. MS-C compiler note 1
  2. 9/23/85 by Tom Thompson
  3.  
  4. The C compiler has some problems converting near code pointers to far code
  5. pointers.  The extract from a .COD file below illustrates the problem.
  6.  
  7. void f(void);            /* f - fwd def of near func */
  8. void (*fp)(void);        /* fp - near pointer to func */
  9. void (far *fpf)(void);   /* fpf - far pointer to func */
  10.  
  11.     fpf = ( void (far *)(void) ) f;
  12.           mov ax,OFFSET _f
  13.           mov WORD PTR _fpf,ax
  14.           mov WORD PTR _fpf+2,cs     ;uses CS as desired
  15.     fpf = ( void (far *)(void) ) fp;
  16.           mov ax,_fp
  17.           mov WORD PTR _fpf,ax
  18.           mov WORD PTR _fpf+2,ds     ;uses DS, not desired
  19.  
  20. The first statement works as desired.  It sets fpf's ofs and seg to point
  21. to the function f.  In the case where the right argument is a variable, it
  22. doesn't work as wanted -- it uses DS instead of CS.  I can't decide whether
  23. this is a bug or just a limitation.  Since this is a small model program, the
  24. compiler ought to know that all code refs are to current CS, however, if you
  25. assume any model possible, then you cannot construct a far pointer from a near
  26. with any certainy.
  27.  
  28. I ran into this problem when passing a near func ptr to a function that
  29. subsequently converted it to a long pointer. eg:
  30.  
  31. void fx(fp)
  32. void (*fp)(void);
  33. {
  34.     void (far *fpf)(void);
  35.     ...
  36.     fpf = ( void (far *)(void) ) fp;   /* generated wrong code */
  37.     ...
  38. }
  39.  
  40. The workaround, VALID ONLY FOR SMALL MODEL, is:
  41.  
  42. void fx(fp)
  43. void (*fp)(void);
  44. {
  45.     void (far *fpf)(void);
  46.     ...
  47.     fpf = fx;     /* fix cs part of fpf */
  48.     FP_OFS(fpf) = (unsigned) fp;   /* put in desired offset */
  49.     ...
  50. }